home *** CD-ROM | disk | FTP | other *** search
/ Java Developer's Companion / Java Developer's Companion.iso / documentation / tutorial / java / threads / example / SortItem.java < prev    next >
Encoding:
Java Source  |  1997-07-13  |  6.0 KB  |  250 lines

  1. /*
  2.  * Copyright (c) 1995-1997 Sun Microsystems, Inc. All Rights Reserved.
  3.  *
  4.  * Permission to use, copy, modify, and distribute this software
  5.  * and its documentation for NON-COMMERCIAL purposes and without
  6.  * fee is hereby granted provided that this copyright notice
  7.  * appears in all copies. Please refer to the file "copyright.html"
  8.  * for further important copyright and licensing information.
  9.  *
  10.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  11.  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  12.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  13.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  14.  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  15.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  16.  */
  17. /*
  18.  * @(#)SortItem.java    1.17f 95/04/10 James Gosling
  19.  *
  20.  * Copyright (c) 1994 Sun Microsystems, Inc. All Rights Reserved.
  21.  *
  22.  * Permission to use, copy, modify, and distribute this software
  23.  * and its documentation for NON-COMMERCIAL purposes and without
  24.  * fee is hereby granted provided that this copyright notice
  25.  * appears in all copies. Please refer to the file "copyright.html"
  26.  * for further important copyright and licensing information.
  27.  *
  28.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  29.  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  30.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  31.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  32.  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  33.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  34.  */
  35.  
  36. import java.awt.*;
  37. import java.io.InputStream;
  38. import java.util.Hashtable;
  39. import java.net.*;
  40.  
  41. /**
  42.  * A simple applet class to demonstrate a sort algorithm.
  43.  * You can specify a sorting algorithm using the "alg"
  44.  * attribyte. When you click on the applet, a thread is
  45.  * forked which animates the sorting algorithm.
  46.  *
  47.  * @author James Gosling
  48.  * @version     1.17f, 10 Apr 1995
  49.  */
  50. public class SortItem extends java.applet.Applet implements Runnable {
  51.     /**
  52.      * The thread that is sorting (or null).
  53.      */
  54.     private Thread kicker;
  55.  
  56.     /**
  57.      * The array that is being sorted.
  58.      */
  59.     int[] arr;
  60.  
  61.     /**
  62.      * The high water mark.
  63.      */
  64.     int h1 = -1;
  65.  
  66.     /**
  67.      * The low water mark.
  68.      */
  69.     int h2 = -1;
  70.  
  71.     /**
  72.      * The name of the algorithm.
  73.      */
  74.     String algName;
  75.  
  76.     /**
  77.      * The sorting algorithm (or null).
  78.      */
  79.     SortAlgorithm algorithm;
  80.  
  81.     /**
  82.      * Fill the array with random numbers from 0..n-1.
  83.      */
  84.     void scramble() {
  85.     int[] a = new int[size().height / 2];
  86.     double f = size().width / (double) a.length;
  87.     for (int i = a.length; --i >= 0;) {
  88.         a[i] = (int)(i * f);
  89.     }
  90.     for (int i = a.length; --i >= 0;) {
  91.         int j = (int)(i * Math.random());
  92.         int t = a[i];
  93.         a[i] = a[j];
  94.         a[j] = t;
  95.     }
  96.     arr = a;
  97.     }
  98.  
  99.     /**
  100.      * Pause a while.
  101.      * @see SortAlgorithm
  102.      */
  103.     void pause() {
  104.     pause(-1, -1);
  105.     }
  106.  
  107.     /**
  108.      * Pause a while, and draw the high water mark.
  109.      * @see SortAlgorithm
  110.      */
  111.     void pause(int H1) {
  112.     pause(H1, -1);
  113.     }
  114.  
  115.     /**
  116.      * Pause a while, and draw the low&high water marks.
  117.      * @see SortAlgorithm
  118.      */
  119.     void pause(int H1, int H2) {
  120.     h1 = H1;
  121.     h2 = H2;
  122.     if (kicker != null) {
  123.         repaint();
  124.     }
  125.     try {Thread.sleep(20);} catch (InterruptedException e){}
  126.     }
  127.  
  128.     /**
  129.      * Initialize the applet.
  130.      */
  131.     public void init() {
  132.     String at = getParameter("alg");
  133.     if (at == null) {
  134.         at = "BubbleSort";
  135.     }
  136.  
  137.     algName = at + "Algorithm";
  138.     scramble();
  139.  
  140.     resize(100, 100);
  141.     }
  142.  
  143.     /**
  144.      * Paint the array of numbers as a list
  145.      * of horizontal lines of varying lenghts.
  146.      */
  147.     public void paint(Graphics g) {
  148.     int[] a = arr;
  149.     int y = size().height - 1;
  150.  
  151.     // Erase old lines
  152.     g.setColor(Color.lightGray);
  153.     for (int i = a.length; --i >= 0; y -= 2) {
  154.         g.drawLine(arr[i], y, size().width, y);
  155.     }
  156.  
  157.     // Draw new lines
  158.     g.setColor(Color.black);
  159.     y = size().height - 1;
  160.     for (int i = a.length; --i >= 0; y -= 2) {
  161.         g.drawLine(0, y, arr[i], y);
  162.     }
  163.  
  164.     if (h1 >= 0) {
  165.         g.setColor(Color.red);
  166.         y = h1 * 2 + 1;
  167.         g.drawLine(0, y, size().width, y);
  168.     }
  169.     if (h2 >= 0) {
  170.         g.setColor(Color.blue);
  171.         y = h2 * 2 + 1;
  172.         g.drawLine(0, y, size().width, y);
  173.     }
  174.     }
  175.  
  176.     /**
  177.      * Update without erasing the background.
  178.      */
  179.     public void update(Graphics g) {
  180.     paint(g);
  181.     }
  182.  
  183.     /**
  184.      * Run the sorting algorithm. This method is
  185.      * called by class Thread once the sorting algorithm
  186.      * is started.
  187.      * @see java.lang.Thread#run
  188.      * @see SortItem#mouseUp
  189.      */
  190.     public void run() {
  191.     try {
  192.         if (algorithm == null) {
  193.         algorithm = (SortAlgorithm)Class.forName(algName).newInstance();
  194.         algorithm.setParent(this);
  195.         }
  196.         algorithm.init();
  197.         algorithm.sort(arr);
  198.     } catch(Exception e) {
  199.     }
  200.     }
  201.  
  202.     /**
  203.      * Stop the applet. Kill any sorting algorithm that
  204.      * is still sorting.
  205.      */
  206.     public synchronized void stop() {
  207.     if (kicker != null) {
  208.             try {
  209.         kicker.stop();
  210.             } catch (IllegalThreadStateException e) {
  211.                 // ignore this exception
  212.             }
  213.         kicker = null;
  214.     }
  215.     if (algorithm != null){
  216.             try {
  217.         algorithm.stop();
  218.             } catch (IllegalThreadStateException e) {
  219.                 // ignore this exception
  220.             }
  221.     }
  222.     }
  223.  
  224.  
  225.     /**
  226.      * For a Thread to actually do the sorting. This routine makes
  227.      * sure we do not simultaneously start several sorts if the user
  228.      * repeatedly clicks on the sort item.  It needs to be
  229.      * synchronoized with the stop() method because they both
  230.      * manipulate the common kicker variable.
  231.      */
  232.     private synchronized void startSort() {
  233.     if (kicker == null || !kicker.isAlive()) {
  234.         scramble();
  235.         repaint();
  236.         kicker = new Thread(this);
  237.         kicker.start();
  238.     }
  239.     }
  240.  
  241.  
  242.     /**
  243.      * The user clicked in the applet. Start the clock!
  244.      */
  245.     public boolean mouseUp(java.awt.Event evt, int x, int y) {
  246.     startSort();
  247.     return true;
  248.     }
  249. }
  250.